// Copyright (c) 2025, 大连理工大学 (Dalian University of Technology)
//
// Licensed under the Mulan PSL v2.
// You can use this file according to the terms and conditions of the Mulan PSL v2.
// You may obtain a copy of the License at
//
//     http://license.coscl.org.cn/MulanPSL2
//
// THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
// EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. SEE THE MULAN PSL v2
// FOR MORE DETAILS.

/*
 * @Author: qiulin_JXGM ql1746365080@gmail.com
 * @Date: 2024-09-05 14:17:30
 * @LastEditors: wxc superjava@qq.com
 * @LastEditTime: 2024-09-29 16:17:07
 * @FilePath: /SGFEM/DBManager/src/Repository/HDF5/Result/ResultDomainsRepository.cpp
 */

#include "DBManager/Repository/HDF5/Result/ResultDomainsRepository.h"

#include <algorithm>
#include <cstddef>
#include <vector>

#include "DataStructure/Common/Id.h"
#include "DataStructure/Result/ResultDomains.h"

#include "DBManager/Database/HDF5Database.h"
#include "DBManager/Repository/HDF5/Base/HDF5RepositoryMacro.h"
#include "DBManager/Repository/HDF5/Base/SingleRepositoryBase.h"

HDF5_REG_REPOSITORY (ResultDomainsRepository)

using namespace SG::DBManager::HDF5;

using namespace SG::DataStructure;
ResultDomainsRepository::ResultDomainsRepository (HDF5Database& database)
    : SG::DBManager::HDF5::SingleRepositoryBase<Post::ResultDomains> (database)
{}

void ResultDomainsRepository::InitialDatasetInfo ()
{
    RegisterTableInfo<Common::Id> ("/OWN/INDEX/RESULT/DOMAINS", IndexDataset);
    RegisterTableInfo<Post::ResultDomains> ("/SG/RESULT/DOMAINS", MainDataset);

    CreateDatasetInFile ();
}

void ResultDomainsRepository::UpdataMaxDomainId ()
{
    auto dataCount = Size ();
    if (dataCount == 0)
    {
        return;
    }
    SortData ();

    Post::ResultDomains tempData;  // NOLINT(misc-const-correctness)
    GetDataset (MainDataset).select ({ dataCount - 1 }, { 1 }).read (tempData);
    SetMaxId (tempData.m_id);
}

std::vector<SG::DataStructure::Post::ResultDomains> ResultDomainsRepository::GetAllData ()
{
    SortData ();

    std::vector<SG::DataStructure::Post::ResultDomains> returnAlldata;
    GetDataset (MainDataset).read (returnAlldata);
    return returnAlldata;
}